package com.joker.aigc.chat.service.event.listener;


import com.joker.aigc.chat.domain.entity.GroupMember;
import com.joker.aigc.chat.domain.entity.RoomGroup;
import com.joker.aigc.domain.entity.User;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketBaseVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketInvitationVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketMemberChangeVo;
import com.joker.aigc.chat.service.GroupMemberService;
import com.joker.aigc.chat.service.PushService;
import com.joker.aigc.service.UserService;
import com.joker.aigc.chat.service.adapter.MemberAdapter;
import com.joker.aigc.chat.service.event.GroupMemberAddEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 添加群成员监听器
 *
 * @author zhongzb create on 2022/08/26
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class GroupMemberAddListener {

    private final UserService userService;
    private final GroupMemberService groupMemberService;
    private final PushService pushService;


    /**
     * 第一个是发送通知，给所有人收到一个邀请组群的通知
     *
     * @param event 消息
     */
    @Async
    @TransactionalEventListener(classes = GroupMemberAddEvent.class, fallbackExecution = true)
    public void sendAddMsg(GroupMemberAddEvent event) {
        // step 1: 获取 需要发送邀请信息 的人员名单
        List<GroupMember> memberList = event.getMemberList();
        RoomGroup roomGroup = event.getRoomGroup();
        Long inviteUid = event.getInviteUid();
        User inviter = userService.getById(inviteUid);
        // step 2: 找到 发送邀请的人员名单
        List<Long> uidList = memberList.stream().map(GroupMember::getUid).collect(Collectors.toList());
        List<User> member = userService.listByIds(uidList);
        if (!uidList.isEmpty()) {
            //
            WebSocketBaseVo<WebSocketInvitationVo> baseVo = MemberAdapter.buildGroupAddMessage(inviter, member);
            pushService.sendWebSocket(baseVo, uidList);
        }
    }

    /**
     * 给 已经在群里的人 发送 成员变动 消息
     *
     * @param event 消息
     */
    @Async
    @TransactionalEventListener(classes = GroupMemberAddEvent.class, fallbackExecution = true)
    public void sendGroupMemberChangePush(GroupMemberAddEvent event) {
        // step 1: 首先获取 新增的成员
        List<GroupMember> memberList = event.getMemberList();
        // 获取新增成员的 uid
        List<Long> uidList = memberList.stream().map(GroupMember::getUid).collect(Collectors.toList());
        RoomGroup roomGroup = event.getRoomGroup(); //
        // step 2: 获取在群里的 全部的人
        List<Long> memberUidList = groupMemberService.getMemberUidList(roomGroup.getRoomId());

        // step 3:找到 已经在群里的人 ；然后放成员变动消息
        memberUidList.removeAll(uidList);
        if (CollectionUtils.isEmpty(memberList)) {
            // 如果为空。说明是新加的群，不需要发送成员变动的消息
            return;
        }
        // step 4: 然后挨个发送 成员变动的消息
        List<User> users = userService.listByIds(memberUidList);
        users.forEach(user -> {
            WebSocketBaseVo<WebSocketMemberChangeVo> ws = MemberAdapter.buildMemberAddWebSocket(roomGroup.getRoomId(), user);
            pushService.sendWebSocket(ws, user.getId());
        });
    }

}
